namespace SubhadraSolutions.Sharp.Logging.Context
{
    using SubhadraSolutions.Sharp.Logging.Contracts;
    using System.Collections.Generic;

    public sealed class DictionaryGlobalContextStrategy : IClearableGlobalContextStrategy
    {
        #region Fields

        private readonly Dictionary<string, object> _dictionary = new Dictionary<string, object>();

        #endregion Fields

        #region Indexers

        public object this[string name]
        {
            get
            {
                object o;
                _dictionary.TryGetValue(name, out o);
                return o;
            }
            set
            {
                if (value == null)
                {
                    _dictionary.Remove(name);
                }
                else
                {
                    _dictionary[name] = value;
                }
            }
        }

        #endregion Indexers

        #region Methods

        public void Clear()
        {
            _dictionary.Clear();
        }

        public void Remove(string name)
        {
            _dictionary.Remove(name);
        }

        #endregion Methods
    }
}